package com.jcf.springboot.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.vo.LoginVo;
import com.jcf.springboot.entity.sys.Manager;
import com.jcf.springboot.security.model.SecurityUser;
import com.jcf.springboot.security.util.JwtUtil;
import com.jcf.springboot.service.ManagerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-03-18
 */
@CrossOrigin
@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private ManagerService managerService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        QueryWrapper<Manager> managerQueryWrapper = new QueryWrapper<>();
        managerQueryWrapper.eq("manager_name", loginVo.getManagerName());
        Manager one = managerService.getOne(managerQueryWrapper);
        if (one != null && passwordEncoder.matches(loginVo.getManagerPassword(),one.getManagerPassword())) {
           Map map = managerService.sealData(one);
            return R.ok().data(map).message("管理员登录成功");
        } else {
            return R.error().message("用户名或密码输入错误");
        }
    }

    @GetMapping("getList")
    @ApiOperation("我是管理员列表接口")
    public R getList(String query,Integer pagenum,Integer pagesize) {
        Page<Manager> page = new Page<>(pagenum, pagesize);
        managerService.getListWithPage(page,query);

        List<Manager> list = page.getRecords();
        long total = page.getTotal();
        Map<Object, Object> map = new HashMap<>();
        map.put("value",list);
        map.put("total", total);
        if (!StringUtils.isEmpty(list)) {
            return R.ok().message("获取用户列表成功").data(map);
        } else {
            return R.error().message("获取用户列表失败");
        }

    }

    @GetMapping("getAll")
    @ApiOperation("我是管理员列表接口")
    public R getList() {
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("manager_id","manager_name", "avatar", "manager_phone", "manager_email","realname","enable");
        SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryWrapper.ne("manager_name",securityUser.getUsername());

        List<Manager> list = managerService.list(queryWrapper);
        if (!StringUtils.isEmpty(list)) {
            return R.ok().message("获取用户列表成功").data(list);
        } else {
            return R.error().message("获取用户列表失败");
        }

    }

    @PostMapping("addManager")
    public R addManager(@RequestBody Manager manager) {
        manager.setManagerPassword(passwordEncoder.encode("991006"));
        boolean b =managerService.saveUser(manager);
        if (b) {
            return R.ok().message("添加管理员成功");
        } else {
            return R.error().message("添加管理员失败");
        }
    }

    @RequestMapping("getManagerById")
    public R getManagerById(@RequestParam String id){
        Manager manager = managerService.getById(id);
        if(!StringUtils.isEmpty(manager)){
            return R.ok().message("获取管理员信息成功").data(manager);
        }
        else{
            return R.error().message("获取管理员信息失败");
        }
    }

    @PostMapping("editManager")
    public R editManager(@RequestBody Manager manager){
        boolean flag = managerService.updateUser(manager);
        if(flag){
            return R.ok().message("编辑成功");
        } else {
            return R.error().message("编辑失败");
        }

    }

    @PostMapping("updateInfo")
    public R updateInfo(@RequestBody Manager manager){
        boolean flag = managerService.updateById(manager);
        if(flag){
            return R.ok().message("编辑成功");
        } else {
            return R.error().message("编辑失败");
        }

    }

    @DeleteMapping("deleteManager/{id}")
    public R deleteManager(@PathVariable String id){
        boolean b = managerService.removeUser(id);

        if(b){
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }



    @DeleteMapping("batchDelete")
    public R batchDelete(@RequestParam("idList") List<String> idList){
        boolean b = managerService.removeByIds(idList);
        if(b){
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }





}

